
package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.ComOrganizationAttach;
import com.hoshiicloud.user.param.organization.ComOrganizationAttachAddParam;
import com.hoshiicloud.user.param.organization.ComOrganizationAttachQueryParam;
import com.hoshiicloud.user.param.organization.ComOrganizationAttachUpdateParam;
import com.hoshiicloud.user.service.organization.ComOrganizationAttachService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * 机构附件com_organization_attach 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */

@Slf4j
@RestController
@RequestMapping("/organization/com-organization-attach")
@Api(value = "SAAS租户供应商附件", tags = {"SAAS租户供应商附件"})
@ApiSort(11)
public class ComOrganizationAttachController {
    @Autowired
    private ComOrganizationAttachService comOrganizationAttachService;


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询机构附件com_organization_attach
     */
    @ApiOperation(value = "分页查询机构附件com_organization_attach")
    @PostMapping("/getPage")
    public Response page(@RequestBody ComOrganizationAttachQueryParam param) {
        try {
            QueryWrapper<ComOrganizationAttach> queryWrapper = new QueryWrapper<ComOrganizationAttach>();
            queryWrapper.lambda()
                    .eq((param.getVendorId() != null && param.getVendorId() != 0), ComOrganizationAttach::getVendorId, param.getVendorId())
                    .eq((param.getAttachmentTypeId() != null && param.getAttachmentTypeId() != 0), ComOrganizationAttach::getAttachmentTypeId, param.getAttachmentTypeId())
                    .eq(StringUtils.isNotBlank(param.getAttachUrl()), ComOrganizationAttach::getAttachUrl, param.getAttachUrl())
                    .eq(StringUtils.isNotBlank(param.getAttachName()), ComOrganizationAttach::getAttachName, param.getAttachName())
                    .eq(StringUtils.isNotBlank(param.getExpirationDate()), ComOrganizationAttach::getExpirationDate, param.getExpirationDate());
            Page<ComOrganizationAttach> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(comOrganizationAttachService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询机构附件com_organization_attach异常", e);
            return Response.failed("分页查询机构附件com_organization_attach异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询机构附件com_organization_attach
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询机构附件com_organization_attach")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(comOrganizationAttachService.getById(id));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增机构附件com_organization_attach")
    public Response save(@RequestBody @Valid ComOrganizationAttachAddParam param) {
        ComOrganizationAttach data;
        try {
            data = BeanUtils.covertType(param, ComOrganizationAttach.class);
        } catch (Exception e) {
            log.error("新增机构附件com_organization_attach Bean转换异常", e);
            return Response.failed("新增机构附件com_organization_attach Bean转换异常");
        }
        return Response.data(comOrganizationAttachService.save(data));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改机构附件com_organization_attach
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改机构附件com_organization_attach")
    public Response update(@RequestBody @Valid ComOrganizationAttachUpdateParam param) {
        ComOrganizationAttach data;
        try {
            data = BeanUtils.covertType(param, ComOrganizationAttach.class);
        } catch (Exception e) {
            log.error("修改机构附件com_organization_attach Bean转换异常", e);
            return Response.failed("修改机构附件com_organization_attach Bean转换异常");
        }
        return Response.data(comOrganizationAttachService.updateById(data));
    }

    @PostMapping("/insertAttach4ComOrganization")
    @ApiOperation(value = "批量新增租户供应商附件com_organization_attach")
    public Response insertAttach4ComOrganization(@RequestBody @Valid List<ComOrganizationAttach> list){
        return Response.data(comOrganizationAttachService.insertAttach4ComOrganization(list));
    }

    @DeleteMapping("/deleteByIds")
    @ApiOperation(value = "批量物理删除租户供应商附件com_organization_attach")
    public Response deleteByIds(@RequestBody List<Long> ids){
        return Response.data(comOrganizationAttachService.deletedByIds(ids));
    }
}

